<template>
      <template v-for="v in list" :key="v.id">
        <template v-if="!v.children || (v.children && v.children.length <= 0)">
            <a-menu-item :key="v.id" @click="handleClickMenu(v, $event)">
                <template #icon v-if="v.icon"><component :is="v.icon"></component></template>
                {{v.title}}
            </a-menu-item>
        </template>
        <template v-else>
            <a-sub-menu :key="v.id">
                <template #icon v-if="v.icon"><component :is="v.icon"></component></template>
                <template #title>{{v.title}}</template>
                <dynamic-sub-menu :list="v.children"></dynamic-sub-menu>
            </a-sub-menu>
        </template>
    </template>
</template>

<script lang="ts">
interface IMenuItem {
  id: string,
  icon: string,
  title: string,
  path: string,
  children?: IMenuItem[]
}
import {
  HomeOutlined,
  TeamOutlined,
  SettingOutlined,
} from "@ant-design/icons-vue";
import { defineComponent, PropType } from 'vue'
import { useRouter } from "vue-router";
export default defineComponent({
    name: "DynamicSubMenu",
    components: {
        HomeOutlined,
        TeamOutlined,
        SettingOutlined,
    },
    props: {
        list: {
          required: true,
          type: Array as PropType<IMenuItem[]>,
          default() {
              return []
          }
      }
    },
    setup(){
        const router = useRouter()
        function handleClickMenu(item:IMenuItem) {
            router.push(item.path)
        }
        return {   
            handleClickMenu
        }
    }
})
</script>

<style>

</style>